<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/7
 * Time: 19:22
 */

namespace app\api\controller;



use think\Db;

class Dyn extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 进入发布页面时调用，用于判断是否有草稿
     * @return \think\response\Json
     */
    public function haveDraft(){
        $isHave = Db::table('up_hot')
            ->field('h_id, h_image, h_title, h_address')
            ->where('h_user_id', $this->user_id)
            ->where('h_type', 2)
            ->find();
        if($isHave){ // 有草稿
            $have = 1;
            $images = trim($isHave['h_image'], ',');
            $isHave['h_image'] = explode(',', $images);
        }else{ // 没有草稿
            $have = 2;
            $isHave = [];
        }
        return json(['status' => 200, 'msg' => $isHave, 'isHave' => $have]);
    }
    /**
     * 删除草稿
     * @return \think\response\Json
     */
    public function delCaoGao(){
        $isHave = Db::table('up_hot')
            ->field('h_id')
            ->where('h_user_id', $this->user_id)
            ->where('h_type', 2)
            ->find();
        if($isHave){
            $res = Db::table('up_hot')->delete($isHave['h_id']);
            if($res){
                return json(['status' => 200, 'msg' => true]);
            }else{
                return json(['status' => 0, 'msg' => false]);
            }
        }else{
            return json(['status' => 200, 'msg' => true]);
        }
    }
    /**
     * 发布||草稿箱
     * @return \think\response\Json
     * h_title        标题
     * h_image        图片
     * h_address      地址
     * h_type         类型   1 发布   2 草稿箱
     * h_id           第一次发布时为0，草稿箱发布时传记录ID
     */
    public function userPublish(){
        $h_title = trim(input('h_title'));
        $h_image = input('h_image');
        $h_address = input('h_address');
        $h_type = input('h_type'); // 1 发布  2 草稿箱
        $h_id = input('h_id'); // 第一次发布时传0，草稿箱发布时传正常ID
        
        $data = [
            'h_image' => $h_image,
            'h_title' => $h_title,
            'h_address' => $h_address,
            'h_user_id' => $this->user_id,
            'h_type' => $h_type
        ];
        if($h_type == 1){
            $data['h_time'] = $this->create_time;
        }
        
        if($h_id != 0){
            $res = Db::table('up_hot')->where('h_id', $h_id)->update($data);
        }else{
            $res = Db::table('up_hot')->insert($data);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 动态列表
     * @return \think\response\Json
     * hot_type     0 全部  1 热门  2 关注
     * page         页码
     */
    public function dynList(){
        $hot_type = input('hot_type'); // 0 全部  1 热门  2 关注
        $where['h_type'] = 1;
        $where['h_status'] = 2;
        $order = 'h_time desc';
        if($hot_type == 1){
            $begin_time = date('Y-m-d H:i:s', strtotime('-7 days'));
            $where['h_time'] = ['>', $begin_time];
            $order = 'h_message desc';
        }
        if($hot_type == 2){
            $users = Db::table('up_like')->where('l_user_id', $this->user_id)->column('l_other_user_id');
            $where['h_user_id'] = ['IN', $users];
        }
        $list = Db::table('up_hot')
            ->join('up_u_user', 'h_user_id = user_id', 'left')
            ->field('user_username, user_image, h_time, h_title, h_image, h_address, h_id, user_id')
            ->where($where)
            ->page($this->page, 10)
            ->order($order)
            ->select();
        if($list){
            foreach($list as $k => $v){
                $img = trim($v['h_image'], ',');
                $list[$k]['h_image'] = explode(',', $img);
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 举报类型
     * @return \think\response\Json
     */
    public function reportType(){
        $list = Db::table('up_report_type')->field('rt_id, rt_name')->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 举报
     * @return \think\response\Json
     * report_hot_id      被举报的动态ID
     * report_type_id     举报类型  逗号隔开
     * report_content     举报内容
     * report_image       举报图片
     */
    public function report(){
        $report_hot_id = input('report_hot_id');
        $report_type_id = input('report_type_id');
        $report_content = input('report_content');
        $report_image = input('report_image');
        $insert = [
            'report_hot_id' => $report_hot_id,
            'report_type_id' => $report_type_id,
            'report_content' => $report_content,
            'report_image' => $report_image,
            'report_user_id' => $this->user_id,
            'report_time' => $this->create_time
        ];
        $res = Db::table('up_report')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    
    /**
     * 关注/取消关注
     * @return \think\response\Json
     * other_user_id    动态发布者ID
     */
    public function userLike(){
        $other_user_id = input('other_user_id'); // 动态发布者ID
        $have = Db::table('up_like')
            ->where('l_user_id', $this->user_id)
            ->where('l_other_user_id', $other_user_id)
            ->field('l_id')
            ->find();
        if($have){
            $res = Db::table('up_like')->where('l_id', $have['l_id'])->delete();
        }else{
            $insert = [
                'l_user_id' => $this->user_id,
                'l_other_user_id' => $other_user_id,
                'l_time' => $this->create_time
            ];
            $res = Db::table('up_like')->insert($insert);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 动态详情
     * @return \think\response\Json
     * h_id   动态ID
     * page   页码
     */
    public function dynDetail(){
        $h_id = input('h_id');
        // 详情
        $detail = Db::table('up_hot')
            ->join('up_u_user', 'h_user_id = user_id', 'left')
            ->field('user_username, user_image, h_time, h_title, h_image, h_address, h_id, user_id, 
            h_message')
            ->where('h_id', $h_id)
            ->find();
        if($detail){
            // 评论
            $comment = Db::table('up_hot_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->field('user_username, user_image, comment_content, comment_id, comment_time')
                ->where('comment_hot_id', $h_id)
                ->order('comment_time desc')
                ->page($this->page, 10)
                ->select();
            if($comment){
                // 回复数量
                foreach($comment as $k => $v){
                    $count = Db::table('up_hot_comment_tt')
                        ->where('comtt_comment_id', $v['comment_id'])
                        ->count();
                    $comment[$k]['reply_num'] = $count;
                }
            }else{
                $comment = [];
            }
            $detail['comment'] = $comment;
            
            // 是否关注
            $isLike = Db::table('up_like')
                ->where('l_user_id', $this->user_id)
                ->where('l_other_user_id', $detail['user_id'])
                ->find();
            if($isLike){
                $like = 1; // 已关注
            }else{
                $like = 2; // 未关注
            }
            $detail['like'] = $like;
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 100, 'msg' => '此动态已不存在']);
        }
    }
    
    /**
     * 发布评论
     * @return \think\response\Json
     * commnet_hot_id    动态ID
     * comment_content   评论内容
     */
    public function writeReport(){
        $comment_hot_id = input('comment_hot_id');
        $comment_content = input('comment_content');
        $insert = [
            'comment_hot_id' => $comment_hot_id,
            'comment_content' => $comment_content,
            'comment_reply_user_id' => $this->user_id,
            'comment_time' => $this->create_time
        ];
        $have = Db::table('up_hot')->where('h_id', $comment_hot_id)->field('h_id')->find();
        if(empty($have)){
            return json(['status' => 0, 'msg' => '此条动态已不存在']);
        }
        Db::startTrans();
        try{
            // 评论
            Db::table('up_hot_comment')->insert($insert);
            // 评论数量加一
            Db::table('up_hot')->where('h_id', $comment_hot_id)->setInc('h_message');
            $senduid = Db::table('up_hot')->where('h_id', $comment_hot_id)->value('h_user_id');
            $title = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_username');
            $title .= ' 评论了您的动态';
            allInNotice($title,$comment_content,6,$senduid,$comment_hot_id);
            Db::commit();
            try{
                $this->Jpush(1, $senduid, $title, ['n_id' => $comment_hot_id,'n_type' => 6]);
            }catch(\Exception $e){

            }
            return json(['status' => 200, 'msg' => '评论成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '评论失败']);
        }
    }
    
    /**
     * 回复评论
     * @return \think\response\Json
     * comtt_comment_id     评论ID
     * comtt_content        回复的内容
     */
    public function replyToComment(){
        $comtt_comment_id = input('comtt_comment_id');
        $comtt_content = input('comtt_content');
        $insert = [
            'comtt_comment_id' => $comtt_comment_id,
            'comtt_reply_user_id' => $this->user_id,
            'comtt_content' => $comtt_content,
            'comtt_time' => $this->create_time
        ];
        $res = Db::table('up_hot_comment_tt')->insert($insert);
        if($res){
            $send = Db::table('up_hot_comment')->where('comment_id', $comtt_comment_id)
                ->field('comment_reply_user_id,comment_hot_id')->find();
            $title = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_username');
            $title .= ' 回复了您的评论';
            allInNotice($title,$comtt_content,6,$send['comment_reply_user_id'],$send['comment_hot_id']);
            try{
                $this->Jpush(1, $send['comment_reply_user_id'], $title, ['n_id' => $send['comment_hot_id'],'n_type' => 6]);
            }catch(\Exception $e){

            }
            return json(['status' => 200, 'msg' => '回复成功']);
        }else{
            return json(['status' => 0, 'msg' => '回复失败']);
        }
    }
    
    /**
     * 评论详情
     * @return \think\response\Json
     * comment_id    评论ID
     * page          页码
     */
    public function commentDetail(){
        $comment_id = input('comment_id');
        $detail = Db::table('up_hot_comment')
            ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
            ->field('user_username, user_image, comment_content, comment_time, comment_id')
            ->where('comment_id', $comment_id)
            ->find();
        if($detail){
            // 回复
            $reply = Db::table('up_hot_comment_tt')
                ->join('up_u_user', 'comtt_reply_user_id = user_id', 'left')
                ->where('comtt_comment_id', $comment_id)
                ->field('comtt_id, comtt_content, comtt_time, user_username, user_image')
                ->page($this->page, 10)
                ->select();
            if($reply){
                $detail['reply'] = $reply;
            }else{
                $detail['reply'] = [];
            }
            return json(['status' => 200, 'msg' => $detail]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 对回复进行回复
     * @return \think\response\Json
     * comtt_id     回复ID
     * comtt_content   回复的内容
     */
    public function replyToReply(){
        $comtt_id = input('comtt_id');
        $comtt_content = input('comtt_content');
        // 被回复的记录
        $reply_info = Db::table('up_hot_comment_tt')
            ->join('up_u_user', 'user_id = comtt_reply_user_id', 'left')
            ->where('comtt_id', $comtt_id)
            ->field('comtt_comment_id, comtt_content, user_username,comtt_reply_user_id')
            ->find();
        $content = $comtt_content . '//@' . $reply_info['user_username'] . ':' . $reply_info['comtt_content'];
        $insert = [
            'comtt_comment_id' => $reply_info['comtt_comment_id'],
            'comtt_reply_user_id' => $this->user_id,
            'comtt_content' => $content,
            'comtt_time' => $this->create_time
        ];
        $res = Db::table('up_hot_comment_tt')->insert($insert);
        if($res){
            $hot_id = Db::table('up_hot_comment')->where('comment_id', $reply_info['comtt_comment_id'])
                ->value('comment_hot_id');
            $title = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_username');
            $title .= ' 回复了您的回复';
            allInNotice($title,$content,6,$reply_info['comtt_reply_user_id'],$hot_id);
            try{
                $this->Jpush(1, $reply_info['comtt_reply_user_id'], $title, ['n_id' => $hot_id,'n_type' => 6]);
            }catch(\Exception $e){

            }
            return json(['status' => 200, 'msg' => '回复成功']);
        }else{
            return json(['status' => 0, 'msg' => '回复失败']);
        }
    }
    
    /**
     * 我的关注
     * @return \think\response\Json
     * page   页码
     */
    public function myLike(){
        // 我关注的人的ID
        $users = Db::table('up_like')
            ->where('l_user_id', $this->user_id)
            ->order('l_time desc')
            ->column('l_other_user_id');
        // 我关注的人列表
        $data = Db::table('up_u_user')
            ->where('user_id', 'IN', $users)
            ->field('user_id, user_username, user_image')
            ->page($this->page, 10)
            ->select();
        if($data){
            return json(['status' => 200, 'msg' => $data]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 用户主页
     * @return \think\response\Json
     * other_user_id    用户ID
     * page    页码
     */
    public function userHomePage(){
        $other_user_id = input('other_user_id');
        // 用户基本信息
        $user_info = Db::table('up_u_user')
            ->where('user_id', $other_user_id)
            ->field('user_username, user_image, user_id')
            ->find();
        
        // 我是否关注该用户
        $isLike = Db::table('up_like')
            ->where('l_user_id', $this->user_id)
            ->where('l_other_user_id', $other_user_id)
            ->find();
        if($isLike){
            $like = 1; // 已关注
        }else{
            $like = 2; // 未关注
        }
        $user_info['like'] = $like;
        
        // 该用户关注的人数
        $likeNum = Db::table('up_like')->where('l_user_id', $other_user_id)->count();
        $user_info['likeNum'] = $likeNum;
        
        // 该用户的粉丝数
        $followNum = Db::table('up_like')->where('l_other_user_id', $other_user_id)->count();
        $user_info['followNum'] = $followNum;
        
        // 该用户的全部动态
        $dyn = Db::table('up_hot')
            ->join('up_u_user', 'user_id = h_user_id', 'left')
            ->field('user_username, user_image, h_id, h_image, h_title, h_address, h_time')
            ->where('h_user_id', $other_user_id)
            ->where('h_type', 1)
            ->where('h_status', 2)
            ->order('h_time desc')
            ->page($this->page, 10)
            ->select();
        if($dyn){
            foreach($dyn as $k => $v){
                $img = trim($v['h_image'], ',');
                $dyn[$k]['h_image'] = explode(',', $img);
            }
            $user_info['dyn'] = $dyn;
        }else{
            $user_info['dyn'] = [];
        }
        return json(['status' => 200, 'msg' => $user_info]);
    }
}